package com.ytd.common.net;

import com.ytd.common.bean.BaseResponse;
import com.ytd.common.util.AESUtils;
import com.ytd.common.util.LogUtil;
import com.ytd.common.util.ThreadUtil;
import com.ytd.common.util.TimerManager;
import com.ytd.common.util.YtdConfig;

/**
 * Socket管理请求类
 *
 * @author gaoguanchao
 * @since 2023/3/22 0022
 */
public class SocketManager {
    private YtdSocket mYtdSocket;

    private boolean isChange = false;

    private void handlerData(BaseResponse data) {
        RequestManager.getInstance().processData(data);
    }

    private SocketManager() {
    }

    public static SocketManager getInstance() {
        return ManagerHolder.instance;
    }

    private SocketCallback mCallback = new SocketCallback() {
        @Override
        public void onReadData(BaseResponse data) {
            handlerData(data);
        }

        @Override
        public void init() {
            if (!YtdConfig.isDeviceActivate()) {
                deviceActivate();
            } else {
                synchronizationData();
            }
        }
    };

    private void deviceActivate() {
        RequestManager.getInstance().deviceActivate(new NetCallBack() {
            @Override
            public void onSuccess(Object result) {
                ThreadUtil.executeMainDelayed(() -> mYtdSocket.heartBeat(), 50);
            }

            @Override
            public void onFail(int code, String msg) {
            }
        });
    }

    /**
     * 初始化Socket
     */
    public void initSocket() {
        if (mYtdSocket != null) {
            mYtdSocket.close();
        }
        mYtdSocket = new YtdSocket(mCallback);
    }

    /**
     * 重新设置Socket
     *
     * @param isChange 是否改变
     */
    public void resetSocket(boolean isChange) {
        if (mYtdSocket == null) {
            return;
        }
        if (isChange) {
            this.isChange = true;
            LogUtil.i("resetSocket isChange");
            mYtdSocket.releaseSocket(true);
        } else {
            deviceConfig();
        }
    }

    public void resetHeartTimer() {
        if (mYtdSocket != null) {
            mYtdSocket.resetHeartTimer();
        }
    }

    public void reconnectSocket() {
        if (!YtdConfig.isDeviceActivate()) {
            return;
        }
        if (mYtdSocket == null) {
            return;
        }
        LogUtil.i("resetSocket reconnectSocket");
        mYtdSocket.releaseSocket(true);
    }

    public void networkOffline() {
        if (mYtdSocket == null) {
            return;
        }
        mYtdSocket.sendOffline();
    }

    public boolean isConnect() {
        return mYtdSocket != null && mYtdSocket.isConnect;
    }


    public void synchronizationData() {
        if (isChange) {
            isChange = false;
            deviceConfig();
        }
        syncServerData();
    }

    private void syncServerData() {
        RequestManager.getInstance().getCurrentTime();
        RequestManager.getInstance().getFileUploadUrl();
        RequestManager.getInstance().getDeviceDetail();
        RequestManager.getInstance().getDeviceServerParams();
        RequestManager.getInstance().getConsumeMode();
        TimerManager.getInstance().initData();
    }

    private void deviceConfig() {
        RequestManager.getInstance().deviceConfig();
    }

    public void sendMsgEncrypt(String json) {
        if (mYtdSocket != null) {
            mYtdSocket.sendMsg(AESUtils.upJson(json, AESUtils.AESKey()));
        }
    }

    public void sendMsg(String json) {
        if (mYtdSocket != null) {
            mYtdSocket.sendMsg(json);
        }
    }

    public void closeSocket() {
        if (mYtdSocket != null) {
            mYtdSocket.close();
            mYtdSocket = null;
        }
    }

    private static class ManagerHolder {
        private static SocketManager instance = new SocketManager();
    }
}
